package com.idp.web.service.impl;

import com.idp.common.base.BaseResult;
import com.idp.common.base.BusinessException;
import com.idp.common.constants.CommonConstants;
import com.idp.common.constants.Constants;
import com.idp.common.persistence.Page;
import com.idp.web.dao.DoctorsDao;
import com.idp.web.dto.DoctorAssignListDTO;
import com.idp.web.dto.DoctorListDTO;
import com.idp.web.entity.Doctors;
import com.idp.web.entity.Orders;
import com.idp.web.service.IDoctorsService;
import com.idp.web.service.IOrdersService;
import com.idp.web.system.entity.SysUser;
import com.idp.web.system.service.SysUserService;
import com.idp.web.vo.DoctorSearchVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 医生service实现类
 *
 * @author xubinren
 * @version 1.0
 * @date 2018/2/27
 */
@Service("doctorsService")
public class DoctorsServiceImpl implements IDoctorsService {

    @Resource
    private DoctorsDao doctorsDao;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private IOrdersService ordersService;

    /**
     * 根据条件分页查询医生列表
     *
     * @param doctorSearch 查询信息：姓名/账号/医院名
     * @param page         分页信息
     * @return Page<DoctorListDTO>
     */
    @Override
    public Page<DoctorListDTO> findByPage(DoctorSearchVo doctorSearch, Page<DoctorListDTO> page) {
        page.setResult(doctorsDao.selectPage(page, doctorSearch));
        return page;
    }

    /**
     * 分配医生--医生列表
     *
     * @param doctorSearch 查询信息
     * @param page         分页信息
     * @return Page<DoctorAssignListDTO>
     */
    @Override
    public Page<DoctorAssignListDTO> assignListPage(DoctorSearchVo doctorSearch, Page<DoctorAssignListDTO> page) {
        page.setResult(doctorsDao.assignListPage(doctorSearch, page));
        return page;
    }

    /**
     * 根据条件查询医生列表
     *
     * @param doctors 查询条件
     * @return List<Doctors>
     */
    @Override
    public List<Doctors> findBySearch(Doctors doctors) {
        return doctorsDao.find(doctors);
    }

    /**
     * 通过ID查询医生信息
     *
     * @param id 主键id
     * @return Doctors
     */
    @Override
    public Doctors getById(String id) {
        return doctorsDao.getById(id);
    }

    /**
     * 新增医生信息
     *
     * @param doctors        医生信息
     * @param doctorSearchVo 验证信息
     * @return BaseResult
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public BaseResult add(Doctors doctors, DoctorSearchVo doctorSearchVo) {
        doctors.preInsert(doctorSearchVo.getUserId());
        doctors.setAccountStastus(CommonConstants.DOCTOR_STATUS_ENABLED);
        doctors.setAdmissionsCount(CommonConstants.INT_ZERO);

        Integer changeNum = doctorsDao.add(doctors);
        if (null == changeNum || changeNum < CommonConstants.INT_ONE) {
            return new BaseResult(Constants.RESPONSE_CODE_605, Constants.RESPONSE_MESSAGE_605);
        }

        //同步到用户信息
        SysUser user = new SysUser();
        BeanUtils.copyProperties(doctors, user);
        user.setUserType(CommonConstants.USER_TYPE_DOCTOR);
        user.preInsert(doctors.getCreateUser());
        user.setId(doctors.getId());
        sysUserService.registerAdd(user);

        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * 修改医生信息
     *
     * @param doctors        医生信息
     * @param doctorSearchVo 验证信息
     * @return BaseResult
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public BaseResult update(Doctors doctors, DoctorSearchVo doctorSearchVo) {

        Doctors oldDoctors = getById(doctors.getId());
        doctors.preUpdate(doctorSearchVo.getUserId());

        doctors.setCreateUser(oldDoctors.getCreateUser());
        doctors.setCreateDate(oldDoctors.getCreateDate());
        doctors.setLogicDelete(oldDoctors.getLogicDelete());
        doctors.setAccountStastus(oldDoctors.getAccountStastus());
        doctors.setAdmissionsCount(oldDoctors.getAdmissionsCount());

        Integer changeNum = doctorsDao.update(doctors);
        if (null == changeNum || changeNum < CommonConstants.INT_ZERO) {
            return new BaseResult(Constants.RESPONSE_CODE_605, Constants.RESPONSE_MESSAGE_605);
        }

        //同步到用户信息
        SysUser user = sysUserService.getById(doctors.getId());
        BeanUtils.copyProperties(doctors, user);
        user.setUserType(CommonConstants.USER_TYPE_DOCTOR);
        user.preUpdate(doctors.getCreateUser());
        user.setId(doctors.getId());
        sysUserService.update(user);

        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * 根据主键id删除医生信息
     *
     * @param id 主键id
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public void delete(String id) {
        doctorsDao.delete(id);
    }

    /**
     * 根据主键id删除医生信息
     *
     * @param doctorSearchVo 信息
     * @return boolean
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public boolean delete(DoctorSearchVo doctorSearchVo) {
        if (CommonConstants.HOSPITAL_PAGETYPE.equals(doctorSearchVo.getPageType())) {
            Doctors doctors = getById(doctorSearchVo.getId());
            doctors.preUpdate(doctorSearchVo.getUserId());
            doctors.setDepartmentId(CommonConstants.STRING_BLANK);
            doctors.setDepartmentName(CommonConstants.STRING_BLANK);
            doctors.setHospitalName(CommonConstants.STRING_BLANK);
            doctorsDao.update(doctors);
        } else {
            doctorsDao.delete(doctorSearchVo.getId());
            sysUserService.delete(doctorSearchVo.getId());
        }
        return true;

    }

    /**
     * 修改医生的账户状态
     *
     * @param id     主键id
     * @param userId 操作人id
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public BaseResult changeStatus(String id, String userId) {
        if (StringUtils.isEmpty(id)) {
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        //根据医生id获取对应的医生信息
        Doctors doctors = getById(id.trim());
        //账户状态：1、可使用；0、禁用
        Integer accountStauts = doctors.getAccountStastus();

        //排除错误数据
        if (null == accountStauts || accountStauts < CommonConstants.INT_ZERO || accountStauts > CommonConstants.INT_ONE) {
            return new BaseResult(Constants.RESPONSE_CODE_604, Constants.RESPONSE_MESSAGE_604);
        }

        //修改状态
        if (CommonConstants.DOCTOR_STATUS_ENABLED == accountStauts) {
            //改为禁用
            doctors.setAccountStastus(CommonConstants.DOCTOR_STATUS_DISABLED);
        } else {
            //改为启用
            doctors.setAccountStastus(CommonConstants.DOCTOR_STATUS_ENABLED);
        }

        doctors.preUpdate(userId);
        doctorsDao.update(doctors);

        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

}